package com.jintu.portal.controller;

import com.alibaba.fastjson.JSONObject;
import com.jintu.portal.exception.ServiceException;
import com.jintu.portal.pojo.bean.Pagination;
import com.jintu.portal.pojo.dto.*;
import com.jintu.portal.pojo.entity.UserBalance;
import com.jintu.portal.pojo.entity.UserWithdraw;
import com.jintu.portal.pojo.submit.ResultInfo;
import com.jintu.portal.repository.mysql.dao.BalanceDao;
import com.jintu.portal.repository.mysql.dao.WithdrawDao;
import com.jintu.portal.service.TobUserService;
import com.jintu.portal.utils.StringUtil;
import com.jintu.portal.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/mine")
public class MineController extends BaseController{

    @Autowired
    private TobUserService tobUserService;

    @Autowired
    private BalanceDao balanceDao;

    @Autowired
    private WithdrawDao withdrawDao;

    /**
     * 首页信息查询
     * @return
     * @throws Exception
     */
    @RequestMapping("index")
    @ResponseBody
    public ResultInfo mine() throws Exception {
        PortalInfoDto ret = new PortalInfoDto();
        ret.setBalance(0d);
        ret.setPerBalance(0d);
        ret.setTeamBalance(0d);
        List<MenuTabDto> menuTabDtoList = new ArrayList<>();
//        MenuTabDto dto1 = new MenuTabDto();
//        dto1.setMenuTabName("我的分销");
//        dto1.setMenuTabTag("distri");
//        dto1.setIcon("http://47.97.210.87/icon/distri.png");
//        menuTabDtoList.add(dto1);

//        MenuTabDto dto2 = new MenuTabDto();
//        dto2.setMenuTabName("我的账单");
//        dto2.setMenuTabTag("bill");
//        dto2.setIcon("http://47.97.210.87/icon/bill.png");
//        menuTabDtoList.add(dto2);

//        MenuTabDto dto3 = new MenuTabDto();
//        dto3.setMenuTabName("拉新数据");
//        dto3.setMenuTabTag("new");
//        dto3.setIcon("http://47.97.210.87/icon/new.png");
//        menuTabDtoList.add(dto3);

        MenuTabDto dto4 = new MenuTabDto();
        dto4.setMenuTabName("实名认证");
        dto4.setMenuTabTag("real");
        dto4.setIcon("http://47.97.210.87/icon/real.png");
        menuTabDtoList.add(dto4);

        MenuTabDto dto5 = new MenuTabDto();
        dto5.setMenuTabName("客服中心");
        dto5.setMenuTabTag("serv");
        dto5.setIcon("http://47.97.210.87/icon/serv.png");
        menuTabDtoList.add(dto5);

        MenuTabDto dto6 = new MenuTabDto();
        dto6.setMenuTabName("设置");
        dto6.setMenuTabTag("config");
        dto6.setIcon("http://47.97.210.87/icon/config.png");
        menuTabDtoList.add(dto6);

        ret.setMenuTablistDto(menuTabDtoList);
        return returnSuccess(ret);
    }

    /**
     * 我的分销
     * @return
     * @throws Exception
     */
    @RequestMapping("distri")
    @ResponseBody
    public ResultInfo distri(HttpServletRequest request) throws Exception {
        String token = request.getParameter("token");
        Long userId = UserUtil.tobCurrentUser1(token).getUserId();

        DistriInfoDto ret = new DistriInfoDto();
        ret.setLevel(1);
        ret.setLevelRule("会员等级升级规则：\n" +
                "1、初级分销员  团队直属一级1-199人；\n" +
                "2、中级分销员  团队直属一级200-599人；\n" +
                "3、高级分销员  团队直属一级600人以上。\n");
        ret.setPerNum(3);
        ret.setUrl("http://47.97.210.87/pic/share.png");

        List<GroupMonthCollectDto> groupMonthCollectDtos = new ArrayList<>();
        GroupMonthCollectDto groupMonthCollectDto1 = new GroupMonthCollectDto();
        groupMonthCollectDto1.setMonth("2019-12");
        groupMonthCollectDto1.setTOneinc(155);
        groupMonthCollectDto1.setTOneValid(133);
        groupMonthCollectDto1.setTOneAmount(1220.5d);
        groupMonthCollectDto1.setTSeveninc(155);
        groupMonthCollectDto1.setTSevenValid(133);
        groupMonthCollectDto1.setTSevenAmount(1220.5d);
        groupMonthCollectDtos.add(groupMonthCollectDto1);

        GroupMonthCollectDto groupMonthCollectDto2 = new GroupMonthCollectDto();
        groupMonthCollectDto2.setMonth("2019-11");
        groupMonthCollectDto2.setTOneinc(267);
        groupMonthCollectDto2.setTOneValid(211);
        groupMonthCollectDto2.setTOneAmount(3220.5d);
        groupMonthCollectDto2.setTSeveninc(267);
        groupMonthCollectDto2.setTSevenValid(211);
        groupMonthCollectDto2.setTSevenAmount(3220.5d);
        groupMonthCollectDtos.add(groupMonthCollectDto2);

        GroupMonthCollectDto groupMonthCollectDto3 = new GroupMonthCollectDto();
        groupMonthCollectDto3.setMonth("2019-10");
        groupMonthCollectDto3.setTOneinc(355);
        groupMonthCollectDto3.setTOneValid(312);
        groupMonthCollectDto3.setTOneAmount(4220.5d);
        groupMonthCollectDto3.setTSeveninc(355);
        groupMonthCollectDto3.setTSevenValid(312);
        groupMonthCollectDto3.setTSevenAmount(4220.5d);
        groupMonthCollectDtos.add(groupMonthCollectDto3);


        List<MemberInfoDto> memberInfoDtos = new ArrayList<>();
        MemberInfoDto memberInfoDto1 = new MemberInfoDto();
        memberInfoDto1.setMemberName("黄小");
        memberInfoDto1.setIncome(100);
        memberInfoDto1.setJoinTime("2019-09-22 16:43:12");
        memberInfoDto1.setMemNum(3);
//        memberInfoDto1.setMemberIcon();
        memberInfoDtos.add(memberInfoDto1);

        MemberInfoDto memberInfoDto2 = new MemberInfoDto();
        memberInfoDto2.setMemberName("朱元");
        memberInfoDto2.setIncome(300);
        memberInfoDto2.setJoinTime("2019-09-12 16:43:12");
        memberInfoDto2.setMemNum(6);
//        memberInfoDto1.setMemberIcon();
        memberInfoDtos.add(memberInfoDto2);

        MemberInfoDto memberInfoDto3 = new MemberInfoDto();
        memberInfoDto3.setMemberName("舒黄");
        memberInfoDto3.setIncome(200);
        memberInfoDto3.setJoinTime("2019-10-12 16:43:12");
        memberInfoDto3.setMemNum(4);
//        memberInfoDto1.setMemberIcon();
        memberInfoDtos.add(memberInfoDto3);

        ret.setGroupDataList(groupMonthCollectDtos);
        ret.setGroupMemList(memberInfoDtos);


        return returnSuccess(ret);
    }

    /**
     * 我的账单
     * @return
     * @throws Exception
     */
    @RequestMapping("check")
    @ResponseBody
    public ResultInfo check(@RequestBody MineReqDto req) throws Exception {
        Pagination pagination = new Pagination();

        if(req.getPageNo() != null){
            pagination.setPage(req.getPageNo());
        }

//        List<BillDetailDto> details = new ArrayList<>();
//        BillDetailDto detail1 = new BillDetailDto();
//        detail1.setTitle("分佣提现");
//        detail1.setTypeDes("提现");
//        detail1.setBillTime("2019-12-20 09:00:23");
//        detail1.setMoney(-59.3D);
//        details.add(detail1);
//
//        BillDetailDto detail2 = new BillDetailDto();
//        detail2.setTitle("邀请一级用户");
//        detail2.setTypeDes("收入");
//        detail2.setBillTime("2019-12-16 11:00:25");
//        detail2.setMoney(10.23);
//        details.add(detail2);
//
//        BillDetailDto detail3 = new BillDetailDto();
//        detail3.setTitle("下级用户邀请");
//        detail3.setTypeDes("收入");
//        detail3.setBillTime("2019-12-15 19:01:29");
//        detail3.setMoney(16.49);
//        details.add(detail3);
//
//        BillDetailDto detail4 = new BillDetailDto();
//        detail4.setTitle("下级用户邀请");
//        detail4.setTypeDes("收入");
//        detail4.setBillTime("2019-12-10 19:01:29");
//        detail4.setMoney(32.58);
//        details.add(detail4);
//
//        pagination.setItems(details);
//        pagination.setTotal(4);
//        pagination.setIncome(59.3D);
//        pagination.setExpend(59.3D);
//        pagination.setLimit(10);

        return returnSuccess(pagination);
    }

    /**
     * 拉新数据
     * @return
     * @throws Exception
     */
    @RequestMapping("bill")
    @ResponseBody
    public ResultInfo bill() throws Exception {
        PullNewDto ret = new PullNewDto();
        List<GroupMonthCollectDto> groupMonthCollectDtos = new ArrayList<>();
        GroupMonthCollectDto groupMonthCollectDto1 = new GroupMonthCollectDto();
        groupMonthCollectDto1.setMonth("2019-12");
        groupMonthCollectDto1.setTOneinc(155);
        groupMonthCollectDto1.setTOneValid(133);
        groupMonthCollectDto1.setTOneAmount(1220.5d);
        groupMonthCollectDto1.setTSeveninc(155);
        groupMonthCollectDto1.setTSevenValid(133);
        groupMonthCollectDto1.setTSevenAmount(1220.5d);
        groupMonthCollectDtos.add(groupMonthCollectDto1);

        GroupMonthCollectDto groupMonthCollectDto2 = new GroupMonthCollectDto();
        groupMonthCollectDto2.setMonth("2019-11");
        groupMonthCollectDto2.setTOneinc(267);
        groupMonthCollectDto2.setTOneValid(211);
        groupMonthCollectDto2.setTOneAmount(3220.5d);
        groupMonthCollectDto2.setTSeveninc(267);
        groupMonthCollectDto2.setTSevenValid(211);
        groupMonthCollectDto2.setTSevenAmount(3220.5d);
        groupMonthCollectDtos.add(groupMonthCollectDto2);

        GroupMonthCollectDto groupMonthCollectDto3 = new GroupMonthCollectDto();
        groupMonthCollectDto3.setMonth("2019-10");
        groupMonthCollectDto3.setTOneinc(355);
        groupMonthCollectDto3.setTOneValid(312);
        groupMonthCollectDto3.setTOneAmount(4220.5d);
        groupMonthCollectDto3.setTSeveninc(355);
        groupMonthCollectDto3.setTSevenValid(312);
        groupMonthCollectDto3.setTSevenAmount(4220.5d);
        groupMonthCollectDtos.add(groupMonthCollectDto3);

        ret.setGroupDataList(groupMonthCollectDtos);
        return returnSuccess(ret);
    }

    /**
     * 我的客服
     * @return
     * @throws Exception
     */
    @RequestMapping("service")
    @ResponseBody
    public ResultInfo service() throws Exception {
        MineServiceDto ret = new MineServiceDto();
        ret.setInviter("13830229987");
        ret.setWxServer("jintu-001");
        return returnSuccess(ret);
    }

    /**
     * 修改个人头像
     * @return
     */
    @RequestMapping("/uploadFile")
    @ResponseBody
    public ResultInfo uploadFile(HttpServletRequest request,@RequestParam MultipartFile file) throws Exception {
        String token = request.getParameter("token");

        Map<String,Object> resultMap = new HashMap<String,Object>();
        if(file == null) {
            throw new ServiceException("缺失必要参数");
        }
        Long userId = UserUtil.tobCurrentUser1(token).getUserId();
        String filePath = tobUserService.uploadFile(userId,file);
        resultMap.put("filePath",filePath);
        return returnSuccess(resultMap);
    }

    /**
     * 提现首页
     * @return
     */
    @RequestMapping("/balance")
    @ResponseBody
    public ResultInfo balance(HttpServletRequest request) throws Exception {
        Map<String,Object> resultMap = new HashMap<String,Object>();

        String token = request.getParameter("token");
        Long userId = UserUtil.tobCurrentUser1(token).getUserId();
        UserBalance userBalance = balanceDao.get(userId);

        if(userBalance != null){
            BigDecimal d1 = new BigDecimal(userBalance.getBalance());
            BigDecimal d2 = new BigDecimal (100);
            BigDecimal ret = d1.divide(d2,2,BigDecimal.ROUND_HALF_DOWN);
            resultMap.put("balance",ret.doubleValue());
        }else{
            resultMap.put("balance",0.00);
        }

        return returnSuccess(resultMap);
    }


    /**
     * 支付宝／微信提现
     * @return
     */
    @RequestMapping("/withdraw")
    @ResponseBody
    public ResultInfo withdraw(@RequestBody JSONObject jsonObject,HttpServletRequest request) throws Exception {
        Map<String,Object> resultMap = new HashMap<String,Object>();
        ResultInfo result = new ResultInfo();

        String account = jsonObject.getString("account");
        String amount = jsonObject.getString("amount");
        String name = jsonObject.getString("name");

        if(StringUtil.isBlank(account, amount)) {
            result.setStatus(500);
            result.setMessage("缺少必要参数！");
            result.setBody(resultMap);
            return result;
        }

        String token = request.getParameter("token");
        Long userId = UserUtil.tobCurrentUser1(token).getUserId();

        UserWithdraw withdraw = new UserWithdraw();
        withdraw.setAccount(account);
        withdraw.setAmount(amount);
        withdraw.setName(name);
        withdraw.setUid(userId);
        withdraw.setCreateTime(System.currentTimeMillis());
        withdraw.setStatus(0);
        withdrawDao.insert(withdraw);

        return returnSuccess(resultMap);
    }

}

